<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Kui.KumomadoAbstractContainer');
	/**
	 * @package libraries/kui/containers/window/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoWindow extends KumomadoAbstractContainer {
		/**
		 * @var unknown_type
		 */
		protected $kuis = array();
		/**
		 * @final
		 * @var string
		 */
		protected $ui_name = 'window';
		/**
		 * @var string
		 */
		protected $template_file = 'KumomadoWindow.xsl';
		/**
		 * @var string
		 */
		protected $icon = null;
		/**
		 * @var string
		 */
		protected $title = 'New Window';
		/**
		 * @var integer
		 */
		protected $width = 25;
		/**
		 * @var integer
		 */
		protected $height = 25;
		/**
		 * @var integer
		 */
		protected $min_width = 25;
		/**
		 * @var integer
		 */
		protected $min_height = 25;
		/**
		 * @var integer
		 */
		protected $pos_x = 50;
		/**
		 * @var integer
		 */
		protected $pos_y = 50;
		/**
		 * @var boolean
		 */
		protected $resizeable = true;
		/**
		 * @var boolean
		 */
		protected $minimizeable = true;
		/**
		 * @var boolean
		 */
		protected $maximizeable = true;
		/**
		 * @var boolean
		 */
		protected $closeable = true;
		/**
		 * @var mixed
		 */
		protected $content_data;
		protected $content_file;
		/**
		 * Construct
		 * @param string $id
		 */
		public function __construct($id) {
			parent::__construct();
			$this->setId($id);
		}
		/**
		 * 
		 * @param string $template_file
		 */
		public function setTemplate($template_file) {
			//$this->template_file = $template_file;
		}
		/**
		 * 
		 * @param string $icon
		 */
		public function setIcon($icon) {
			if (file_exists($icon)) {
				$this->icon = $icon;
			}
		}
		/**
		 * 
		 * @return string
		 */
		public function getIcon() {
			if (isset($this->icon)) {
				return $this->icon;
			} else {
				return KUMOMADO_PLATFORM_THEME.'images/kumomado.ico';
			}
		}
		/**
		 * 
		 * @param string $title
		 */
		public function setTitle($title) {
			$this->title = $title;
		}
		/**
		 * 
		 * @param integer $width
		 * @param integer $height
		 */
		public function setSize($width = 30, $height = 20) {
			$this->width = ($width = intval(abs($width))) >= $this->min_width ? $width : $this->min_width;
			$this->height = ($height = intval(abs($height))) >= $this->min_height ? $height : $this->min_height;
		}
		/**
		 * 
		 * @param integer $pos_x
		 * @param integer $pox_y
		 */
		public function setPos($pos_x = 50, $pos_y = 50) {
			$pos_x = intval($pos_x);
			$pos_y = intval($pos_y);
			if ($pos_x > 100) {
				$pos_x = 100;
			}
			if ($pos_y > 100) {
				$pos_y = 100;
			}
			$this->pos_x = $pos_x;
			$this->pos_y = $pos_y;
		}
		/**
		 * 
		 */
		public function setLeft() {
			$this->setPos(0, 0);
		}
		/**
		 * 
		 */
		public function setCenter() {
			$this->setPos(50, 50);
		}
		/**
		 * 
		 */
		public function setRight() {
			$this->setPos(100, 100);
		}
		/**
		 * 
		 * @param boolean $resizeable
		 */
		public function setResizeable($resizeable = true) {
			$this->resizeable = (boolean)$resizeable;
		}
		/**
		 * 
		 * @param boolean $minimizeable
		 */
		public function setMinimizeable($minimizeable = true) {
			$this->minimizeable = (boolean)$minimizeable;
		}
		/**
		 * 
		 * @param boolean $maximizeable
		 */
		public function setMaximizeable($maximizeable = true) {
			$this->maximizeable = (boolean)$maximizeable;
		}
		/**
		 * 
		 * @param boolean $closeable
		 */
		public function setCloseable($closeable = true) {
			$this->closeable = (boolean)$closeable;
		}
		/**
		 * 
		 * @param string $content
		 * @param mixed $content_data
		 * @see ./framework/libraries/kui/KumomadoAbstractContainer#setContent($content, $content_data)
		 */
		public function setContent($content_file, $content_data = null) {
			$this->content_file = $content_file;
			$this->content_data = $content_data;
		}
		/**
		 * 
		 * @return array
		 */
		protected function getAttrs() {
			return array(
				'id' => $this->id,
				'icon' => $this->getIcon(),
				'title' => $this->title,
				'size' => array(
					'width' => $this->width,
					'height' => $this->height,
					'min_width' => $this->min_width,
					'min_height' => $this->min_height
				),
				'pos' => array(
					'x' => (100 - $this->width) * $this->pos_x / 100,
					'y' => (100 - $this->height) * $this->pos_y / 100
				),
				'controll' => array(
					'resizeable' => $this->resizeable,
					'minimizeable' => $this->minimizeable,
					'maximizeable' => $this->maximizeable,
					'closeable' => $this->closeable
				)
			);
		}
		/**
		 * 
		 * @return string Window view of the html code
		 */
		protected function view() {
			$xslt = new KumomadoXsltProcessor();
			$xslt->importArray($this->getAttrs());
			$xslt->importStyleSheet(dirname(__FILE__).DIRECTORY_SEPARATOR.$this->template_file);
			return $xslt->transformToDoc();
		}
		/**
		 * 
		 */
		protected function getContent() {
			if (!file_exists($this->content_file)) {
				return false;
			}
			$ui = array();
			foreach ($this->kuis as $key => $kui) {
				$render = $kui->render();
				$ui['Kui'][$key] = $render['view'];
			}
			if (is_array($this->content_data)) {
				$ui = array_merge($ui, $this->content_data);
			}
			$xslt = new KumomadoXsltProcessor();
			if (is_array($ui)) {
				$xslt->importArray($ui);
			}
			//echo $xslt->xml;
			$xslt->importStyleSheet($this->content_file);
			return $xslt->transformToDoc();
		}
		/**
		 * 
		 * @see ./framework/libraries/kui/KumomadoAbstractContainer#render()
		 * @return array
		 */
		public function render() {
			$this->render = array(
				'ui_name' => $this->ui_name,
				'attrs' => $this->getAttrs(),
				'view' => $this->view(),
				'content' => $this->getContent(),
			);
			$this->KuiClient->register($this->getId(), $this->render);
		}
		/**
		 * @param object $kui @link KumomadoUserInterface
		 */
		public function add(KumomadoUserInterface $kui) {
			if ($kui->isCollecten()) {
				$this->kuis[$kui->getId()] = $kui;
			}
			return $this;
		}
	}
?>